/*******************************************************************************
 * Name    : SplashScreenController.cc
 * Date    : 01-14-2015
 * Author  : Sylvain Nieuwlandt -- nicholas.rush.developper@gmail.com
 ******************************************************************************/

#include <Controllers/MainWindow/SplashScreenController.hpp>

KiLogger* SplashScreenController::LOGGER;

SplashScreenController::SplashScreenController() {
  
}

/******************************************************************************/
SplashScreenController::~SplashScreenController() {

}

/******************************************************************************/
void SplashScreenController::showAndLoadComponents() {
    SplashScreenView splashScreenView;
    splashScreenView.openAndShow();
    
    this->loadGeneralComponents();
    
    splashScreenView.hideAndClose();
}
/******************************************************************************/

void SplashScreenController::loadGeneralComponents() {
    KiString exeDirectory = KiSystem::getExecutableDirectory();
    
    /* Load the configuration properties */
    this->loadConfiguration(exeDirectory);
    /* Load the label properties */
    this->loadLabel(exeDirectory);
    /* Load the cards images */
    this->loadCardsImages(exeDirectory);
}
/******************************************************************************/

void SplashScreenController::loadConfiguration(KiString exeDirectory) {
    KiString* configFilePath = KiString::concat(exeDirectory, CONFIG_FILE);
    try {
        KiInputStringStream configStream(*configFilePath);
        configStream.open();
        ConfigurationManager::initialize(*configStream.getFullContent());
        configStream.close();
    } catch (FileNotFoundException &ex) {
        LOGGER->fatal("Unable to load the config file.", &ex);
        exit(EXIT_FAILURE);
    }
}
/******************************************************************************/

void SplashScreenController::loadLabel(KiString exeDirectory) {
    KiString* labelFilePath = KiString::concat(exeDirectory, LABEL_FILE);
    try {
        KiInputStringStream labelStream(*labelFilePath);
        labelStream.open();
        LabelManager::initialize(*labelStream.getFullContent());
        labelStream.close();
    } catch (FileNotFoundException &ex) {
        LOGGER->fatal("Unable to load the label file.", &ex);
        exit(EXIT_FAILURE);
    }
}
/******************************************************************************/

void SplashScreenController::loadCardsImages(KiString exeDirectory){
    KiString* imagesPath = ConfigurationManager::getInstance()->get(
    		KiString(IMAGES_PATH));
    KiString* imagesType = ConfigurationManager::getInstance()->get(
    		KiString(IMAGES_EXTENSION));
    try {
        KiDirectory imagesDir(*KiString::concat(exeDirectory, *imagesPath));
        KiList cardsPathList = imagesDir.listAll(*
            KiString::concat("*", *imagesType));
        KiListIterator cardsPathIterator = cardsPathList.getIterator();
        while (KiBoolean::TRUE == cardsPathIterator.hasNext()) {
            KiString* path = (KiString*) cardsPathIterator.getNext();
            KiInputByteStream byteStream(*path);
            try {
                byteStream.open();
                KiBufferedImage cardImage(byteStream.getAllBytes());
                CardsImageManager::getInstance().put(*(
                    byteStream.getFileName()->remove(*imagesType)), cardImage);
                byteStream.close();
            } catch (FileNotFoundException &ex) {
                LOGGER->fatal("Unable to load a card's image", &ex);
                exit(EXIT_FAILURE);
            }
        }
    } catch (DirectoryNotExistsException &ex) {
        LOGGER->fatal("Unable to list the images directory", &ex);
        exit(EXIT_FAILURE);
    }
}
/******************************************************************************/
